{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 常量四则运算\n",
    "data1 = tf.constant(6)\n",
    "data2 = tf.constant(2)\n",
    "add = tf.add(data1,data2)\n",
    "mul = tf.multiply(data1,data2)\n",
    "sub = tf.subtract(data1,data2)\n",
    "div = tf.divide(data1,data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "12\n",
      "4\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    print(sess.run(add))\n",
    "    print(sess.run(mul))    \n",
    "    print(sess.run(sub))    \n",
    "    print(sess.run(div))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "12\n",
      "4\n",
      "3.0\n",
      "8\n",
      "14\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "# 变量四则运算\n",
    "data1 = tf.Variable(6)\n",
    "data2 = tf.Variable(2)\n",
    "add = tf.add(data1,data2)\n",
    "# 将add后的结果赋予data2\n",
    "dataCopy = tf.assign(data2,add)\n",
    "mul = tf.multiply(data1,data2)\n",
    "sub = tf.subtract(data1,data2)\n",
    "div = tf.divide(data1,data2)\n",
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    print(sess.run(add))\n",
    "    print(sess.run(mul))    \n",
    "    print(sess.run(sub))    \n",
    "    print(sess.run(div)) \n",
    "    # 使data2 = 8\n",
    "    print(sess.run(dataCopy))\n",
    "    # 运行tensor的另外一种方法，即eval()，通过获取默认的session执行run操作.\n",
    "    # 此处data2 = (data2+data1),即8+6,通过之前的操作data2已经等于8\n",
    "    print(dataCopy.eval())\n",
    "    # 运行tensor的另外一种方法，获取一个默认的session然后执行run方法，data2=6+14\n",
    "    print(tf.get_default_session().run(dataCopy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
